//wontube blog
$(function(){
	blog.run();
});

var blog = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){			
			this.contextPath = $("#contextPath").val();
			this.status = $("#status").val();
		},
		
		_start_: function(){		
			this._list_blog();
		},
		
		_init_events: function(){			
			$("#status").change(function(){
				blog.status = $("#status").val();
				blog._list_blog();
			});
			
			$("#blogCategory").change(function(){
				blog.category = $(this).val();
				blog._list_blog();
			});
			
			this._bind_blog_category_event();
			this._bind_blog_tag_event();			
		},
		
		_bind_blog_category_event: function(){
			$("#add_category_btn").click(function(){
				var data = $("#add_cat_form").serialize();
				var url = blog.contextPath + "/admin/blog/category/add";
				$.post(url, data, function(result){
					alert(result);
					if(result.indexOf('Done') == 0){
						location.reload(true);
					}
				});
			});
			
			$("#category_table tr > td > a.delete").click(function(){
				var cid = $(this).attr("cid");
				var url = blog.contextPath + "/admin/blog/category/delete/" + cid;
				$.post(url, function(result){
					alert(result);
					if(result.indexOf("Done") == 0){
						location.reload(true);
					}
				});
			});
		},
		
		_bind_blog_tag_event: function(){
			$("#add_tag_btn").click(function(){
				var data = $("#add_tag_form").serialize();
				var url = blog.contextPath + "/admin/blog/tag/add";
				$.post(url, data, function(result){
					alert(result);
					if(result.indexOf('Done') == 0){
						location.reload(true);
					}
				});
			});
			
			$("#tag_table tr > td > a.delete").click(function(){
				var cid = $(this).attr("cid");
				var url = blog.contextPath + "/admin/blog/tag/delete/" + cid;
				$.post(url, function(result){
					alert(result);
					if(result.indexOf("Done") == 0){
						location.reload(true);
					}
				});
			});
		},
		
		_list_blog: function(url){
			url = url || this.contextPath + "/admin/blog/query?page=1";
			if(this.status){
				url += "&status=" + this.status;
			}
			if(this.category){
				url += "&category=" + this.category;
			}
			
			$.get(url, function(result){
				$("#blog_list_div").html(result);
				blog._bind_blog_events();
			});
		},
		
		_bind_blog_events: function(){
			$("#blog_table tr > td > a.delete").click(function(){
				var c = confirm("确定删除blog吗?");
				if(c){
					var id = $(this).attr("bid");
					blog._handle_blog_event('delete', id);
				}
			});
			
			$("#blog_table tr > td > a.release").click(function(){
				var c = confirm("确定发布blog吗?");
				if(c){
					var id = $(this).attr("bid");
					blog._handle_blog_event('release', id);
				}
			});
			
			$("#blog_table tr > td > a.cancle").click(function(){
				var c = confirm("确定取消发布blog吗? blog将切换成草稿状态.");
				if(c){
					var id = $(this).attr("bid");
					blog._handle_blog_event('cancle', id);
				}
			});
			
			$("#blog_table tr > td > a.recall").click(function(){
				var c = confirm("确定取消删除blog吗? blog将切换成草稿状态.");
				if(c){
					var id = $(this).attr("bid");
					blog._handle_blog_event('recall', id);
				}
			});			
			
			$("#blog_table tr.paging-tr > td > a").click(function(){
				var url = $(this).attr("href");
				blog._list_blog(url);
				return false;
			});
		},
		
		_handle_blog_event: function(type, id){			
			var url = this.contextPath + "/admin/blog/" + type + "/" + id;
			$.post(url, function(result){
				alert(result);
				if(result.indexOf('Done') == 0){									
					location.reload(true);					
				}
			});
		}
};